package cc.linker.steplink.crm.merchanth5.commons;

import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.commons.model.FormLoginDto;
import cc.linker.steplink.crm.merchanth5.service.MaiBaoLaService;
import cc.linker.steplink.crm.merchanth5.service.redis.CustomerCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: CKQ
 * @DateTime: 2020/10/20 10:45
 * @Description: TODO
 */
@Component
@Slf4j
public class FormFilter {
    @Autowired
    private UserInfoUtil userInfoUtil;
    @Autowired
    private RequestFilter requestFilter;
    @Autowired
    private CustomerCacheService customerCacheService;


    /**
     * @Description：H5获取
     * @DateTime：2020/10/20 10:39
     * @Params：
     * @Return：
     */
    public FormLoginDto getFormLoginDto(HttpServletRequest request, boolean isH5) {
        if (isH5) {
            WxUser wxUser = this.requestFilter.getWxUser(request);
            Map<String, Object> storeInfo = this.customerCacheService.getCustomerCacheStoreInfo(wxUser.getOrgId(), wxUser.getCustomerId(), "2");
            return FormLoginDto.builder()
                    .customerId(wxUser.getCustomerId())
                    .orgId(wxUser.getOrgId())
                    .cover(wxUser.getCover())
                    .customerName(wxUser.getNickname())
                    .customerPhone(storeInfo != null && storeInfo.containsKey("phone") && storeInfo.get("phone") !=null ? storeInfo.get("phone").toString() : wxUser.getBindPhone())
                    .storeName(storeInfo != null && storeInfo.containsKey("storeName") && storeInfo.get("storeName") !=null  ? storeInfo.get("storeName").toString() : "")
                    .wxOpenId(wxUser.getOpenid())
                    .isOrgAccount(0)
                    .wxToken(wxUser.getToken()).build();
        } else {
            PcUserInfo pcUserInfo = this.userInfoUtil.getPcUserInfo(request);
            Boolean isSales = pcUserInfo.getSaler();
            String customerId = isSales ? pcUserInfo.getCustomerId() : pcUserInfo.getOrgId();

            return FormLoginDto.builder()
                    .customerPhone(pcUserInfo.getStorePhone())
                    .customerId(customerId)
                    .orgId(pcUserInfo.getOrgId())
                    .isOrgAccount(pcUserInfo.getSaler() ? 0 : 1)
                    .customerName(pcUserInfo.getAdminName())
                    .wxOpenId(pcUserInfo.getOpenId())
                    .wxToken(pcUserInfo.getToken())
                    .storeName(pcUserInfo.getStoreName()).build();
        }
    }
}
